﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace OkTool.Util
{
    public class CollUtil
    {
        /// <summary>
        /// 是否为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool IsEmpty(IList<object> list)
        {
            return IsEmpty<object>(list);
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool IsEmpty<T>(IList<T> list)
        {
            return list == null || list.Count == 0;
        }

        /// <summary>
        /// 新建一个HashSet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isSorted">是否有序,有序返回,否则返回</param>
        /// <param name="ts"></param>
        /// <returns></returns>
        public static HashSet<T> Set<T>(bool isSorted, params T[] ts)
        {
            if (ts == null)
            {
                return isSorted ? new HashSet<T>() : new HashSet<T>();
            }

            if (!isSorted)
            {
                return new HashSet<T>(ts);
            }
            else
            {
                var result = new HashSet<T>(); 
                var sortedSet = new SortedSet<T>(ts);
                foreach (T item in sortedSet)
                {
                    result.Add(item);
                }
                return result;
            }
           
        }

        /// <summary>
        /// 新建一个HashSet
        /// </summary>
        /// <typeparam name="T">元素类型</typeparam>
        /// <param name="ts">元素数组</param>
        /// <returns></returns>
        public static HashSet<T> NewHashSet<T>(params T[] ts)
        {
            return Set(false, ts);
        }

        /// <summary>
        /// 新建一个HashSet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static HashSet<T> NewHashSet<T>(IEnumerable<T> collection)
        {
            return NewHashSet(false, collection);
        }

        /// <summary>
        /// 新建一个HashSet
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="isSorted"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static HashSet<T> NewHashSet<T>(bool isSorted, IEnumerable<T> collection)
        {
            if (collection == null)
            {
                return isSorted ? new HashSet<T>() : new HashSet<T>();
            }

            if (!isSorted)
            {
                return new HashSet<T>(collection);
            }
            else
            {
                var result = new HashSet<T>();
                var sortedSet = new SortedSet<T>(collection);
                foreach (T item in sortedSet)
                {
                    result.Add(item);
                }
                return result;
            }
        }

        /// <summary>
        /// 两个集合的病机
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="coll1"></param>
        /// <param name="coll2"></param>
        /// <returns></returns>
        public static IEnumerable<T> Union<T>(IEnumerable<T> coll1, IEnumerable<T> coll2)
        {
            if (!coll1.Any() && !coll2.Any())
            {
                return new List<T>();
            }
            if (!coll1.Any())
            {
                return new List<T>(coll2);
            }
            else if (!coll2.Any())
            {
                return new List<T>(coll1);
            }
            return coll1.Union(coll2);
        }
    }
}
